package com.wuhunyu.commonutils.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共返回类
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-12 23:20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult {

    @ApiModelProperty("结果状态码")
    private Integer code;

    @ApiModelProperty("是否成功")
    private Boolean success;

    @ApiModelProperty("返回信息")
    private String message;

    @ApiModelProperty("返回成功的结果集")
    private Map<String, Object> data = new HashMap<>(16);

    /**
     * 返回成功的结果
     * @param msg
     * @param data
     * @return
     */
    public static CommonResult success(String msg,Map<String, Object> data) {
        return new CommonResult(ResultCode.SUCCESS, true, msg, data);
    }

    public static CommonResult success(String msg) {
        return new CommonResult(ResultCode.SUCCESS, true, msg, null);
    }

    public static CommonResult success(Map<String, Object> data) {
        return new CommonResult(ResultCode.SUCCESS, true, "成功", data);
    }

    public static CommonResult success() {
        return new CommonResult(ResultCode.SUCCESS, true, "成功", null);
    }

    /**
     * 返回失败的结果
     * @param msg
     * @return
     */
    public static CommonResult error(String msg) {
        return new CommonResult(ResultCode.ERROR, false, msg, null);
    }

    public static CommonResult error() {
        return new CommonResult(ResultCode.ERROR, false, "失败", null);
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", success=" + success +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
